<template>
  <div class="combuyinfo">
    <div class="Commonread">
      <div v-html="goods_synopsis"></div>
    </div>
    <div class="buyfooter">
      <div class="buymoney">
        <span class="moneynow">¥{{goods.actual_price}}</span>原价<span class="moneyord">¥{{goods.original_price}}</span>{{goods.goods_unit_name}}
      </div>
      <div class="btns">
        <btn :obj='btnstyle' :text="textbtn" v-on:statr="paycommonread" v-if="buy_al===0"></btn>
        <btn :obj='btnstyle_al' :text="textbtn_al" v-else></btn>
      </div>
    </div>
  </div>
</template>

<script>
  import {Indicator, Toast} from 'mint-ui'

  export default {
    name: 'index',
    data: function () {
      return {
        textbtn:'立即加入',
        btnstyle: {
          opacity: 1,
          width: '90%',
          height:'4.5rem',
          background:'#fd2840',
          color:'#fff',
          borderRadius:'.4rem'
        },
        textbtn_al:'已参加该活动',
        btnstyle_al: {
          opacity: 1,
          width: '90%',
          height:'4.5rem',
          background:'#f0f0f2',
          color:'#909099',
          borderRadius:'.4rem'
        },
        goods:{},
        order_id:'',
        goods_synopsis:'',
        buy_al:0
      }
    },
    mounted: function () {
      this.updata()
    },
    methods: {
      updata:function(){
        var goodsparms = {
          goods_id:this.$route.query.goods_id,
          goods_unit_id:this.$route.query.goods_unit_id
        }
        this.$axios.goods(goodsparms).then(res => {
          this.goods_synopsis = this.$Mate.qingReadTextRetuen(res.data.qing_data.goods.goods_synopsis)
          this.goods = res.data.qing_data.goods
          this.buy_al = res.data.qing_data.repeat
        })
      },
      Pay_order: function (data) {
        this.$axios.post_order_activity(data).then(res => {
          // 获取用户邀请码列表需要的orderid
          this.order_id = res.data.qing_data.wx_pay_info.order_id

          var _this = this
          var str = {
            order_id: res.data.qing_data.wx_pay_info.order_id,
            order_no: res.data.qing_data.wx_pay_info.order_id,
            order_type: res.data.qing_data.wx_pay_info.order_type,
            pay_mode_id: res.data.qing_data.wx_pay_info.pay_mode_id
          }
          var obj = {
            timeStamp: res.data.qing_data.wx_pay_info.pay_parameters.timeStamp,
            appId: res.data.qing_data.wx_pay_info.pay_parameters.appId,
            nonceStr: res.data.qing_data.wx_pay_info.pay_parameters.nonceStr,
            package: res.data.qing_data.wx_pay_info.pay_parameters.pkg,
            paySign: res.data.qing_data.wx_pay_info.pay_parameters.paySign,
            pkg: res.data.qing_data.wx_pay_info.pay_parameters.pkg,
            signType: res.data.qing_data.wx_pay_info.pay_parameters.signType,
          }
          // _this.payment_status(str)
          this.$wx.WXPay(obj, res => {
            _this.num = 0
            if (res === 'get_brand_wcpay_request:ok') {
              Indicator.open({
                text: '正在查询支付结果',
                spinnerType: 'fading-circle'
              })
              setTimeout(() => {
                _this.payment_status(str)
              }, 500)
            } else if (res === 'get_brand_wcpay_request:cancel') {
              // _this.payState = true
              Toast({
                message: '支付失败',
                iconClass: 'iconfont icon-bangdingshibai',
                duration: 3000
              })
            } else if (res === 'get_brand_wcpay_request:fail') {
              // _this.payState = true
              Toast({
                message: '支付失败',
                iconClass: 'iconfont icon-bangdingshibai',
                duration: 3000
              })
            }
          })
        })
      },
      payment_status: function (data) {
        this.$axios.inspect(data).then(res => {
          this.num = 0
          Indicator.close()
          if (res.data.qing_code === 100) {
            Toast({
              message: '支付成功',
              iconClass: 'iconfont icon-tianjiachenggong',
              duration: 3000
            })
            this.go('ComreadBuysucess',{ order_id: this.order_id })
            // this.updata({page_index: 1})
          } else {
            Toast({
              message: '支付失败',
              iconClass: 'iconfont icon-bangdingshibai',
              duration: 3000
            })
          }
        })
      },
      paycommonread:function(){
        var parms = {}
        parms.share_id = this.$route.query.share_id || -1
        parms.share_token = this.$route.query.share_token || -1
        parms.goods_id = this.$route.query.goods_id,
        parms.goods_unit_id = this.$route.query.goods_unit_id
        this.$axios.order_activity(parms).then(res => {
          res.data.qing_data.pay_mode_id = 1
          res.data.qing_data.leave_msg = 1
          res.data.qing_data.goods_unit_id = parms.goods_unit_id
          res.data.qing_data.share_id = this.$route.query.share_id || -1
          res.data.qing_data.share_token = this.$route.query.share_token || -1
          this.Pay_order(res.data.qing_data)
        })
      },
      go: function (name, obj) {
        this.$router.push({
          name: name,
          query: obj || {}
        })
      }
    },
    components: {
      btn:()=>import('../../components/btn/index'),
    }
  }
</script>

<style scoped>
  @import "../../assets/css/views/ComreadBuyinfo/index.css";

</style>
